<?php

namespace App\Jobs;

use App\Mail\PassWord;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use \Helper;

class SendEmail implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;
    protected $_admin;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($_userInfo)
    {
        $this->_admin = $_userInfo;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            Mail::to($this->_admin['email'])->send(new PassWord($this->_admin));
            $this->recordLog();
        } catch (\Exception $mailEx) {
            // todo write error to log
            logger($mailEx->getMessage());
        }
    }

    protected function recordLog() {
        $log = Helper::getLog(storage_path('logs/queue.log'), 'info');
        $content = array_filter($this->_admin, function($value, $key) {
            if (in_array($key, ['_id', 'name', 'email'])) {
                return true;
            }
        }, ARRAY_FILTER_USE_BOTH);
        $log->info('sendEmail', $content);
    }
}
